TARGET		:= riscv64gc-unknown-none-elf
MODE		:= debug
KERNEL_FILE	:= target/$(TARGET)/$(MODE)/os
BIN_FILE	:= target/$(TARGET)/$(MODE)/kernel.bin

OBJDUMP		:= rust-objdump --arch-name=riscv64
OBJCOPY		:= rust-objcopy --binary-architecture=riscv64

.PHONY: doc kernel build clean qemu run

# 默认build，输出为二进制文件
build: $(BIN_FILE)

# 通过Rust 文件中的注释生成os文档
doc:
	@cargo doc --document-private-items

# 编译kernel
kernel:
	@cargo build

# 生成kernel 的二进制文件
$(BIN_FILE): kernel
	@$(OBJCOPY) $(KERNEL_FILE) --strip-all -O binary $@ 

# 查看反汇编结果
asm:
	@$(OBJDUMP) -d $(KERNEL_FILE) | less

# 清除文件 clean
clean:
	@cargo clean

# 运行QEMU
qemu: build
	@qemu-system-riscv64 \
			-machine virt \
			-nographic \
			-bios default \
			-device loader,file=$(BIN_FILE),addr=0x80200000

# 默认使用run进行运行
run: build qemu